<!DOCTYPE html>
<html xmlns:shiro="http://www.pollix.at/thymeleaf/shiro"
      xmlns:th="http://www.thymeleaf.org">
<head th:include="/include/commonHeader::commonHeader('人员冲突列表')"></head>
<body class="gray-bg layer-blue">
<div class="wrapper wrapper-content animated fadeInRight full-container">
    <div class="row" id="container">
        <div class="ibox float-e-margins">
            <div class="ibox-content">
                <div class="btn-group table-tool" id="exampleToolbar" role="group">
                    <input id="query_techNames" type="text" class="table-tool-btn pull-left"
                           placeholder="排班授权"/>

                    <button type="button" onclick="reload()" class="btn btn-outline btn-success "
                            data-toggle="tooltip" title="刷新">
                        <i class="fa fa-refresh" aria-hidden="true"></i>
                    </button>
                    <button type="button" onclick="create()" class="btn btn-outline btn-success "
                            data-toggle="tooltip" title="新增">
                        <i class="fa fa-plus" aria-hidden="true"></i>
                    </button>
                </div>
                <table id="exampleTable" data-classes="table table-hover table-condensed">
                </table>
            </div>
        </div>
    </div>
</div>
<script src="/js/jquery.min.js"></script>
<!--<script src="/plugins/bootstrap/js/bootstrap.min.js"></script>-->
<script src="/plugins/bootstraptable/bootstrap-table.min.js"></script>
<script src="/plugins/bootstraptable/locale/bootstrap-table-zh-CN.min.js"></script>
<script src="/plugins/bootstrapValidator/js/bootstrapValidator.min.js"></script>
<script src="/plugins/bootstrapValidator/js/language/zh_CN.js"></script>
<script src="/plugins/layer/layer.js"></script>
<script type="text/javascript">
    /*var p_delete = 'hidden';
    var p_edit = 'hidden';*/
    var p_delete = '';
    var p_edit = '';
</script>
<shiro:hasPermission name="basicinfo:authorizedStrength:delete">
    <script type="text/javascript">
        p_delete = '';
    </script>
</shiro:hasPermission>
<shiro:hasPermission name="basicinfo:authorizedStrength:edit">
    <script type="text/javascript">
        p_edit = '';
    </script>
</shiro:hasPermission>

<!-- 自定义js -->
<script th:inline="javascript">
    var prefix = "/authorizedStrength";
    var moduleFlag = "[[${moduleFlag}]]".substr(1,1);
    $(function () {
        load();
    });
    function load() {
        $('#exampleTable').bootstrapTable(
            {
                method: 'post',
                url: prefix + "/list.do",
                //iconSize: 'outline',
                toolbar: '#exampleToolbar',
                striped:false,
                showRefresh:true,
                dataType:'json',
                locale:"zh-CN",
                pagination:false,//是否分页
                /*pageList:[15,30,50],//可选每页页数
                pageSize:15,//每页记录行数
                pageNumber:1,//初始化加载第一页
                sidePagination:'server',//分页方式*/
                contentType: "application/x-www-form-urlencoded",
                queryParams: function (params) {
                    return {
                        moduleFlag: moduleFlag,
                        techNames: $("#query_techNames").val(),
                        /*limit: params.limit,
                        offset: params.offset*/
                    };
                },
                onClickRow:function(row, $element){
                    $element.addClass('info').siblings().removeClass('info').end();
                },
                onDblClickRow: function (row, $element, field) {
                    view(row.id);
                },
                columns: [
                    {
                        field: 'crewName',
                        title: '机组编制',
                        valign: 'middle',
                    },
                    {
                        field: 'serialNo',
                        title: '号位'
                    },
                    {
                        field: 'rankName',
                        title: '机上岗位'
                    },
                    {
                        field: 'techNames',
                        title: '排班授权'
                    },
                    {
                        title: '操作',
                        align: 'center',
                        visible: isShowOperation(),
                        formatter: function (value, row, index) {
                            return [
                                '<a class="' + p_edit + '" href="#" title="编辑" onclick="edit(' + row.crewId+','+row.serialNo + ')">',
                                '<i class="glyphicon glyphicon-edit"></i>',
                                '</a>&nbsp;&nbsp;',
                                '<a class="' + p_delete + '" href="#" title="删除" onclick="remove(' + row.crewId+','+row.serialNo + ')">',
                                '<i class="glyphicon glyphicon-remove text-danger"></i>',
                                '</a>&nbsp;&nbsp;'
                            ].join('');
                        }
                    }],
                onLoadSuccess : function (data) {
                    var data = $('#exampleTable').bootstrapTable('getData', true);
                    //合并单元格
                    mergeCells(data, "crewName", 1, $('#exampleTable'));
                }
            })
    }

    //是否有权限显示 '操作' 列
    function isShowOperation() {
        return (p_edit != 'hidden');
    }

    //刷新
    function reload() {
        $("#exampleTable").bootstrapTable('refresh');
    }
    //新建
    function create() {
        layer.open({
            type: 2,
            title: '编辑',
            shadeClose: false, // 点击遮罩关闭层
            area: ['50%', '60%'],
            content: prefix + '/toAddPage.do?moduleFlag='+moduleFlag
        });
    }

    //编辑
    function edit(crewId,serialNo) {
        layer.open({
            type: 2,
            title: '编辑',
            shadeClose: false, // 点击遮罩关闭层
            area: ['50%', '60%'],
            content: prefix + '/toEditPage.do?crewId=' + crewId+'&serialNo='+serialNo +'&moduleFlag='+moduleFlag
        });
    }
    //删除
    function remove(crewId,serialNo){
        layer.confirm('确定要删除选中的记录？', {
            btn: ['确定', '取消']
        }, function (index) {
            $.ajax({
                cache: true,
                type: "POST",
                url: prefix + "/delete.do",
                data: {
                    crewId: crewId,
                    serialNo: serialNo,
                },
                success: function (data) {
                    toastr.success("删除成功");
                    layer.close(index);
                    $("#exampleTable").bootstrapTable('refresh');
                }
            });
        });
    }
    function orderFormatter(value, row, index) {
        return index + 1;
    }

    function mergeCells(data,fieldName,colspan,target){
        //声明一个map计算相同属性值在data对象出现的次数和
        var sortMap = {};
        for(var i = 0 ; i < data.length ; i++){
            for(var prop in data[i]){
                if(prop == fieldName){
                    var key = data[i][prop]
                    if(sortMap.hasOwnProperty(key)){
                        sortMap[key] = sortMap[key] * 1 + 1;
                    } else {
                        sortMap[key] = 1;
                    }
                    break;
                }
            }
        }
        for(var prop in sortMap){
            console.log(prop,sortMap[prop])
        }
        var index = 0;
        for(var prop in sortMap){
            var count = sortMap[prop] * 1;
            $(target).bootstrapTable('mergeCells',{index:index, field:fieldName, colspan: colspan, rowspan: count});
            index += count;
        }
    }

</script>
</body>
</html>